package Dao;
import Model1.goods;
import Model1.staff;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
//import java.sql.*;
import java.util.*;
public class staffDao {
	private static PreparedStatement ps=null;
	private static ResultSet rs=null;
	private static Connection conn=null;
	
	public static Boolean Insert(staff stf) {
		if(stf==null)
			return false;
		conn = DatabaseConn.getConn();
		String sql = "insert into staff(name,id,level,phonenumber,password,image) values(?,?,?,?,?,?)";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, stf.getName());
			ps.setInt(2, stf.getId());
			ps.setString(3, stf.getLevel());
			ps.setString(4, stf.getPhonenumber());
			ps.setString(5, stf.getPassword());
			ps.setBinaryStream(6, stf.getImage());
			int i = ps.executeUpdate();
			
			DatabaseConn.close(conn, ps, rs);
			return i==1;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public static staff SearchById(int id) {
		if(id<=0)
			return null;
		conn = DatabaseConn.getConn();
		staff stf = null;
		String sql = "select * from staff where id like "+id;
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while(rs.next()) {
				stf = new staff(rs.getString("name"),rs.getInt("id"),rs.getString("level"),rs.getString("phonenumber"),rs.getString("password")+"");
				stf.setImage(rs.getBinaryStream("image"));
			}
			DatabaseConn.close(conn, ps, rs);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return stf;
	}
	
	public static Boolean Login(int id,String password,String level) {
		if(id<0)
			return false;
		conn=DatabaseConn.getConn();
		String sql="select * from staff where id = "+id+" and password ='"+password+"'and level='"+level+"'";
		try {
			ps=conn.prepareStatement(sql);
			rs = ps.executeQuery();
			Boolean flag=false;
			while(rs.next()) {
				
				flag=true;
			}
			DatabaseConn.close(conn, ps, rs);
			return flag;
		}catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public static Boolean Delete(int id) {
		if(id<=0)
			return false;
		conn = DatabaseConn.getConn();
		String sql = "delete from staff where id like "+id;
		try {
			ps = conn.prepareStatement(sql);
			int i =ps.executeUpdate();
			DatabaseConn.close(conn, ps, rs);
			return i==1;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public static Boolean Update(staff stf) {
		conn = DatabaseConn.getConn();
		String sql = "update staff set name=?,level=?,phonenumber=?,password=?,image=? where id=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, stf.getName());
			ps.setString(2, stf.getPassword());
			ps.setString(3, stf.getLevel());
			ps.setString(4, stf.getPhonenumber());
			ps.setBinaryStream(5, stf.getImage());
			ps.setInt(6, stf.getId());
			int i = ps.executeUpdate();
			DatabaseConn.close(conn, ps, rs);
			return i==1;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public static List<staff> findAll() {
		conn = DatabaseConn.getConn();
		List<staff> stf=new ArrayList<>();
		String sql = "select * from staff";
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while(rs.next()) {
				stf.add(new staff(rs.getString("name"),rs.getInt("id"),rs.getString("level"),rs.getString("phonenumber"),rs.getString("password")+""));
				stf.get(stf.size()-1).setImage(rs.getBinaryStream("image"));
			}
			DatabaseConn.close(conn, ps, rs);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return stf;
	}

	
}
